package net.zjitc.dao.Impl;

import net.zjitc.dao.CategoryDao;
import net.zjitc.pojo.Category;
import net.zjitc.utils.db.C3P0Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by dell on 2017/11/18.
 */
public class CategoryDaoImpl implements CategoryDao {
    QueryRunner runner=new QueryRunner(C3P0Utils.getDataSource());
    public List<Category> findAllCategory() {
        String sql="select * from category";
        try {
            List<Category> list= (List<Category>) runner.query(sql,new BeanListHandler(Category.class));
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void categoryAdd(Category category) {
        String sql="insert into category (cname) values(?)";
        try {
            runner.update(sql,category.cname);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public Category findCategoryById(String cid) {
        String sql="select * from category where cid =?";
        try {
            Category category= (Category) runner.query(sql, new BeanHandler(Category.class),cid);
            return category;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void categoryEdit(Category category) {
        String sql="update category  set cname=? where cid =?";
        try {
            runner.update(sql,category.cname,category.cid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void CategoryDelete(String cid) {
        String sql="delete from category where cid =?";
        try {
            runner.update(sql,cid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
